﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Drawing;
using System.Web;
using System.Web.SessionState;

namespace qf_cms.admin
{
	public partial class verifycode : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				GenImg(this.GetCode(4));
		}

		//产生随机字符串 
		private string GetCode(int num)
		{
			//string[] source = { "0","1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

			string[] source = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
			string code = "";
			Random rd = new Random();
			for (int i = 0; i < num; i++)
			{
				code += source[rd.Next(0, source.Length)];
			}
			return code;
		}

		//生成图片 
		private void GenImg(string code)
		{
			if (code.Equals(""))
				return;

			Random rd = new Random();
			Bitmap myPalette = new Bitmap(90, 25); //定义一个画板 
			Graphics gh = Graphics.FromImage(myPalette); //在画板上定义绘图的实例 
			try
			{
				Rectangle rc = new Rectangle(0, 0, 90, 25); //定义一个矩形 

				//String picPath = Server.MapPath("pic/bg" + rd.Next(1, 4).ToString().Trim() + ".jpg");
				//Bitmap imagefile = (Bitmap)System.Drawing.Image.FromFile(picPath, true); //得到一张位图 
				//TextureBrush texture = new TextureBrush(imagefile); //以图片建立绘图刷 

				Color[] fontcolor = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Red, Color.Brown, Color.DarkCyan, Color.Purple }; //定义 8 种颜色 
				String[] fontname = { "Verdana", "System", "Comic Sans MS", "Arial", "宋体" }; //定义 5 种字体 
				Font myfont; //字体定义 
				SolidBrush mybrush; //画笔定义 

				//gh.FillRectangle(texture, rc);//使用绘图刷填充矩形，到此得到图片背景 

				gh.FillRectangle(new SolidBrush(System.Drawing.Color.WhiteSmoke), 0, 0, 90, 25);

				for (short i = 0; i <= code.Length - 1; i++)
				{
					myfont = new Font(fontname[rd.Next(0, 5)], 11, FontStyle.Italic); //随机字体,42号，斜体 
					mybrush = new SolidBrush(fontcolor[rd.Next(0, 8)]); //随机颜色 
					gh.DrawString(code.Substring(i, 1), myfont, mybrush, 3 + (i * 21), rd.Next(1, 3));//在矩形内画出字符串 
				}

				//myPalette = TwistImage(myPalette, true, 3, 1);

				//画图片的前景噪音点 
				/*for (int i = 0; i < 30; i++)
				{
					int x = rd.Next(myPalette.Width);
					int y = rd.Next(myPalette.Height);
					myPalette.SetPixel(x, y, Color.FromArgb(rd.Next()));
					//myPalette.SetPixel(x, y, Color.Gainsboro);
				}*/

				myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来 

				Session["VerifyCode"] = code; //将字符串保存到Session中，以便需要时进行验证 

			}
			finally
			{
				gh.Dispose();
				myPalette.Dispose();
			}
		}

		private void CreateCheckCodeImage(string checkCode)
		{
			if (checkCode == null || checkCode.Trim() == String.Empty)
				return;
			System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 15.5)), 25);
			Graphics g = Graphics.FromImage(image);
			try
			{
				//生成随机生成器 
				Random random = new Random();
				//清空图片背景色 
				g.Clear(Color.White);
				//画图片的背景噪音线 
				for (int i = 0; i < 5; i++)
				{
					int x1 = random.Next(image.Width);
					int x2 = random.Next(image.Width);
					int y1 = random.Next(image.Height);
					int y2 = random.Next(image.Height);
					g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
				}
				Font font = new System.Drawing.Font("Arial", 15, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
				System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
				g.DrawString(checkCode, font, brush, 2, 2);
				//画图片的前景噪音点 
				for (int i = 0; i < 50; i++)
				{
					int x = random.Next(image.Width);
					int y = random.Next(image.Height);
					image.SetPixel(x, y, Color.FromArgb(random.Next()));
				}
				//画图片的边框线 
				g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
				System.IO.MemoryStream ms = new System.IO.MemoryStream();
				image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
				Response.ClearContent();
				Response.BinaryWrite(ms.ToArray());
			}
			finally
			{
				g.Dispose();
				image.Dispose();
			}
		}

		/**/
		/// <summary>
		/// 正弦曲线Wave扭曲图片
		/// </summary>
		/// <param name="srcBmp">图片路径</param>
		/// <param name="bXDir">如果扭曲则选择为True</param>
		/// <param name="nMultValue">波形的幅度倍数，越大扭曲的程度越高，一般为3</param>
		/// <param name="dPhase">波形的起始相位，取值区间[0-2*PI)</param>
		/// <returns></returns>
		public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
		{
			System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);

			// 将位图背景填充为白色
			System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
			graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
			graph.Dispose();

			double PI2 = System.Math.PI * 2;
			double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;

			for (int i = 0; i < destBmp.Width; i++)
			{
				for (int j = 0; j < destBmp.Height; j++)
				{
					double dx = 0;
					dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
					dx += dPhase;
					double dy = Math.Sin(dx);

					// 取得当前点的颜色
					int nOldX = 0, nOldY = 0;
					nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
					nOldY = bXDir ? j : j + (int)(dy * dMultValue);

					System.Drawing.Color color = srcBmp.GetPixel(i, j);
					if (nOldX >= 0 && nOldX < destBmp.Width
						&& nOldY >= 0 && nOldY < destBmp.Height)
					{
						destBmp.SetPixel(nOldX, nOldY, color);
					}
				}
			}

			return destBmp;
		}
	}
}